package com.flying.invest.strategy.service;

import java.util.Date;

import com.flying.common.annotation.DaoCreate;
import com.flying.common.annotation.DaoQuery;
import com.flying.common.annotation.Transaction;
import com.flying.common.util.Codes;
import com.flying.framework.annotation.MethodInfo;
import com.flying.framework.annotation.Param;
import com.flying.framework.annotation.Service;
import com.flying.framework.model.ActionResult;
import com.flying.framework.model.ModelFactory;
import com.flying.framework.model.QueryResult;
import com.flying.framework.service.AbstractService;
import com.flying.invest.invest.TransactionType;
import com.flying.invest.stat.service.WaveStrategyStageStatService;
import com.flying.invest.strategy.model.StrategyGridTransaction;

@Service(value="StrategyGridTransactionService", desc="网格相关交易")
public class StrategyGridTransactionService extends AbstractService{
	private WaveStrategyStageStatService statServce;
	private StrategyGridDetailService detailService;
	
	public void setStrategyStageStatService(WaveStrategyStageStatService s) {
		this.statServce = s;
	}
	public void setStrategyGridDetailService(StrategyGridDetailService s) {
		this.detailService = s;
	}
        
	@MethodInfo("新增")
	@DaoCreate(entity=StrategyGridTransaction.ENTITY_NAME)
	public StrategyGridTransaction create(
		
			@Param(value=StrategyGridTransaction.TRANSACTION_ID, required=true,	desc="交易ID") Long transaction_id,
			@Param(value=StrategyGridTransaction.DETAIL_ID, required=true,	desc="明细ID") Long detail_id,
			@Param(value=StrategyGridTransaction.USER_ID, required=true,	desc="所属用户") Long user_id,
			@Param(value=StrategyGridTransaction.TYPE_ID, required=true,	desc="策略类型") Long type_id,
			@Param(value=StrategyGridTransaction.INVEST_ID, required=true,	desc="投资项目") Long invest_id,
			@Param(value=StrategyGridTransaction.STRATEGY_ID, required=true,	desc="策略ID") Long strategy_id,
			@Param(value=StrategyGridTransaction.TRANSACTION_TYPE, required=false,	desc="交易类型") Integer transaction_type,
			@Param(value=StrategyGridTransaction.AMOUNT, required=false,	desc="交易金额") Double amount,
			@Param(value=StrategyGridTransaction.QUANTITY, required=false,	desc="交易数量") Double quantity,
			@Param(value=StrategyGridTransaction.PRICE, required=false,	desc="交易价格") Double price,
			@Param(value=StrategyGridTransaction.CREATE_TIME, required=false,	desc="创建时间") Date create_time
		
		) {
		
		return null;
	}

	@MethodInfo("删除")
	@Transaction
	public ActionResult initStageStat(
			@Param(value=StrategyGridTransaction.INVEST_ID, required=true,	desc="投资项目") Long invest_id) {
		var list = this.findByInvestId(invest_id).getRows();
		list.stream().forEach(t -> {
			if(t.getTransactionType() == TransactionType.BUY.value) {
				var detail = this.detailService.findById(t.getDetailId());
				this.statServce.createForBuy(t.getUserId(), t.getInvestId(), t.getTypeId(), t.getStrategyId(), t.getDetailId(), 
						t.getPrice(), t.getAmount(), t.getQuantity(), t.getTransactionId(), detail.getGridBuyPrice(), new Date());
			} else if(t.getTransactionType() == TransactionType.SELL.value) {
				var stat = this.statServce.findForSell(t.getStrategyId(), t.getDetailId());
				this.statServce.updateForSell(stat.getStatId(), t.getPrice(), t.getAmount(), t.getQuantity(), 
						t.getTransactionId(), new Date(), t.getAmount() - stat.getBuyAmount());
			}
		});
		return ModelFactory.create(ActionResult.class, Codes.CODE, Codes.SUCCESS, Codes.EFFECT_ROWS, list.size());
	}

	@MethodInfo("删除")
	@Transaction
	public ActionResult initAllStageStat() {
		var list = this.findAll(0, 10000).getRows();
		list.stream().forEach(t -> {
			if(t.getTransactionType() == TransactionType.BUY.value) {
				var detail = this.detailService.findById(t.getDetailId());
				this.statServce.createForBuy(t.getUserId(), t.getInvestId(), t.getTypeId(), t.getStrategyId(), t.getDetailId(), 
						t.getPrice(), t.getAmount(), t.getQuantity(), t.getTransactionId(), detail.getGridBuyPrice(), t.getCreateTime());
			} else if(t.getTransactionType() == TransactionType.SELL.value) {
				var stat = this.statServce.findForSell(t.getStrategyId(), t.getDetailId());
				this.statServce.updateForSell(stat.getStatId(), t.getPrice(), t.getAmount(), t.getQuantity(), 
						t.getTransactionId(), t.getCreateTime(), t.getAmount() - stat.getBuyAmount());
			}
		});
		return ModelFactory.create(ActionResult.class, Codes.CODE, Codes.SUCCESS, Codes.EFFECT_ROWS, list.size());
	}

	@MethodInfo("搜索")
	@DaoQuery(entity=StrategyGridTransaction.ENTITY_NAME, pageable=false, modelClass=StrategyGridTransaction.class)
	public QueryResult<StrategyGridTransaction> findByInvestId(
			@Param(value=StrategyGridTransaction.INVEST_ID, required=true,	desc="投资项目") Long invest_id) {
		return null;
	}

	@MethodInfo("搜索")
	@DaoQuery(entity=StrategyGridTransaction.ENTITY_NAME, pageable=true, modelClass=StrategyGridTransaction.class)
	public QueryResult<StrategyGridTransaction> findAll(
			@Param(value=Codes.PAGE_NUM, 	required=false, desc="分页页号，默认0") int page,
			@Param(value=Codes.PAGE_SIZE, 	required=false, desc="分页大小，默认10") int rows) {
		return null;
	}

	@MethodInfo("查询")
	@DaoQuery(entity=StrategyGridTransaction.ENTITY_NAME, single=true, throwsNotFoundException=true )
	public StrategyGridTransaction findById(
		
			@Param(value=StrategyGridTransaction.TRANSACTION_ID, required=true,	desc="交易ID") Long transaction_id
		) {
		return null;
	}

}